package com.changgou.seckill.task;

import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;

import java.util.Date;

/**
 * @ClassName MultiThreadingCreateOrder
 * @Description
 * @Author Mr.L
 * @Date 14:06 2019/11/28
 * @Version 2.1
 **/
public class MultiThreadingCreateOrder {


    @Autowired(required = false)
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    /**
     * @param
     * @return void
     * @author 栗子
     * @Description 异步执行
     * @Date 22:49 2019/8/27
     **/
    @Async
    public void createOrder() {

        try {
            // 用户队列
            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();
            Long seckillId = seckillStatus.getGoodsId();
            String userId = seckillStatus.getUsername();
            String time = seckillStatus.getTime();

            // 购买商品，判断队列中是否还有商品id
            Object goodId = redisTemplate.boundListOps("SeckillGoodsCountList_" + seckillId).rightPop();
            if (goodId == null){
                // 队列中无该商品：删除排队信息、删除重复下单信息
                redisTemplate.boundHashOps("UserQueueStatus").delete(userId);
                redisTemplate.boundHashOps("UserQueueCount_" + seckillId).delete(userId);
            }

            // 从redis中获取秒杀商品
            SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time).get(seckillId);
            if (seckillGoods == null) {
                throw new RuntimeException("对不起，该商品已售罄");
            }
            // 如果没有被抢购完，可以进行下单操作
            SeckillOrder seckillOrder = new SeckillOrder();
            seckillOrder.setId(idWorker.nextId());  // 主键
            seckillOrder.setSeckillId(seckillId);   // 秒杀商品id
            seckillOrder.setMoney(seckillGoods.getCostPrice()); // 支付金额
            seckillOrder.setUserId(userId);         // 当前用户
            seckillOrder.setCreateTime(new Date()); // 下单时间
            seckillOrder.setStatus("0");            // 支付状态：未支付
            // 将秒杀订单信息存储到redis中
            redisTemplate.boundHashOps("SeckillOrder").put(userId, seckillOrder);

            // 扣减秒杀商品的库
            seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);   // 只能秒杀一件商品
            if (seckillGoods.getStockCount() >= 0) { // 有货
                // 更新redis中的商品数量
                redisTemplate.boundHashOps("SeckillGoods_" + time).put(seckillId, seckillGoods);
            } else { // 商品卖完了
                // 删除Redis中商品
                redisTemplate.boundHashOps("SeckillGoods_" + time).delete(seckillId);
                // 将该商品同步到mysql中
                seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
            }

            // 抢单成功，更新用户的抢单状态
            seckillStatus.setStatus(2); // 排队--->待支付
            seckillStatus.setOrderId(seckillOrder.getId());  // 订单id
            seckillStatus.setMoney(Float.valueOf(seckillOrder.getMoney())); // 订单金额
            redisTemplate.boundHashOps("UserQueueStatus").put(userId, seckillStatus);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



